import cookie from '../lib/cookie';
import CONSTANT from '../constant/common';


const setToken = (token) => {
  cookie(CONSTANT.COOKIE_TOKEN, token, { maxage: 7 * 24 * 60 * 60 * 1000 });
};

const getToken = () => {
  const ret = cookie(CONSTANT.COOKIE_TOKEN) || '';
  return ret;
};

  // https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
  // https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE&component_appid=component_appid#wechat_redirect

const wechatAuthUrl = ({
  appid, redirectUri, scope, componentAppid,
}) => `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=0&component_appid=${componentAppid}#wechat_redirect`;

const hasOpenId = () => cookie(CONSTANT.COOKIE_USER_OPENID);

export default {
  hasOpenId,
  setToken,
  getToken,
  wechatAuthUrl,
};
